package leetcode.code;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution475 implements IDebug {

	public int findRadius(int[] houses, int[] heaters) {
		Arrays.sort(houses);
		Arrays.sort(heaters);
		int ans = 0;
		for (int i = 0, j = 0; i < houses.length; i++) {
			while (!isBest(houses, heaters, i, j)) {
				j++;
			}
			ans = Math.max(ans, Math.abs(houses[i] - heaters[j]));
		}
		return ans;
	}

	private boolean isBest(int[] houses, int[] heaters, int i, int j) {
		return j == heaters.length - 1 || Math.abs(houses[i] - heaters[j]) < Math.abs(houses[i] - heaters[j + 1]);
	}

	@Override
	public void debug1() {
		String str1 = "[1,999]";//[1,1,1,1,1,1,999,999,999,999,999]
		String str2 = "[499,500,501]";
		int e = 498;
		int r = this.findRadius(HelpDebug.str2array3(str1), HelpDebug.str2array3(str2));
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution475 so = new Solution475();
		so.debug1();

	}

}
